package com.enjoyread.api.controller;

import com.enjoyread.common.config.SystemConfig;
import com.enjoyread.core.manager.intf.AliOssService;
import com.enjoyread.common.utils.RUtils;
import com.enjoyread.common.vo.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/api/oss/")
@CrossOrigin//实现跨域
@Api(value = "实现资源操作",tags = "实现资源操作相关接口")
public class AliOssController {
    @Autowired
    private AliOssService service;
//    private Object request;

    @PostMapping("uploadHeadImg")
    public R upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        System.err.println(file.getOriginalFilename());
        if(file!=null &&!file.isEmpty()) {
            return service.uploadHead(request.getIntHeader(SystemConfig.HEADER_TOKEN_UID),file.getOriginalFilename(), file.getBytes());
        }else {
            return RUtils.fail("请选择上传的图片");
        }
    }
}
